import { isBlank, isFlag } from '@/utils/util'
import store from '@/store/index'

export default {}

/**
 * 跳转页面离开时的缓存方法
 * */
export const beforeRouteLeave = (to, from, next, routerPage) => {
  if (isFlag(routerPage.indexOf(to.name), -1)) {
    from.meta.noCache = false
  } else {
    from.meta.noCache = true
  }
  next()
}

/**
 * 进入页面时的查询缓存方法
 * */
export const beforeRouteEnter = (to, from, next) => {
  next(vm => {
    var router = vm.routerPage
    if (router.indexOf(from.name) > -1) {
      next()
    } else {
      Object.assign(vm.$data, vm.$options.data.call(vm))
      vm.change(1)
    }
  })
}

/**
 * 进入页面时的查询缓存方法
 * */
export const beforeRouteEnterCopy = (to, from, next) => {
  next(vm => {
    var router = vm.routerPage
    if (router.indexOf(from.name) > -1) {
      next()
    } else {
      // 对分页和条件搜索进行进行初始化，不进行其他操作
      Object.assign(vm.$data, vm.$options.data.call(vm))
      if (isBlank(from.name)) {
        vm.initFunctions()
        store.state.initDataFlag = false
        setTimeout(() => {
          store.state.initDataFlag = true
        }, 300)
      }
    }
  })
}
